package it.polito.connectedcar.message;

import java.net.InetSocketAddress;

/**
 * Abstract class wrapping the network level datagram /frame of whatever. It
 * simply contains some common information to {@link IncomingMessage} and
 * {@link OutgoingMessage}.
 * 
 * @author Nicola Aresta
 * 
 */
public abstract class Message {

	private InetSocketAddress sourceAddress;
	private InetSocketAddress tccAddress;
	private boolean forTCC;
	private boolean forZones;

	protected static final int TOKEN_LENGTH = 4;
	protected int sourceTypeCode;
	protected int actionCode;
	protected Storage storage;
	protected long timestamp;

	protected Message(byte[] data, byte sourceTypeCode) {
		this.sourceTypeCode = sourceTypeCode;
		storage = new Storage(data);
	}

	protected Message(byte sourceTypeCode, byte messageCode) {
		this.sourceTypeCode = sourceTypeCode;
		this.actionCode = messageCode;
		storage = new Storage();
	}

	public void empty() {
		storage.reset();
	}

	public void destroy() {
		empty();
		storage = null;
		sourceAddress = null;
	}

	public long getTimestamp() {
		return this.timestamp;
	}

	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}

	public boolean isForZones() {
		return forZones;
	}

	public void setForZones(boolean forZones) {
		this.forZones = forZones;
	}

	public boolean isForCurrentTCC() {
		return forTCC;
	}

	public void setForTCC(boolean forTCC) {
		this.forTCC = forTCC;
	}

	public InetSocketAddress getTccAddress() {
		return tccAddress;
	}

	public void setTccAddress(InetSocketAddress tccAddress) {
		this.tccAddress = tccAddress;
	}

	public InetSocketAddress getSourceAddress() {
		return sourceAddress;
	}

	public void setSourceAddress(InetSocketAddress sourceAddress) {
		this.sourceAddress = sourceAddress;
	}
}
